from django.db import models


# 节点表
class NodeModel(models.Model):
    name = models.CharField(max_length=50, verbose_name='节点名称')

    class Meta:
        db_table = 'node'
        verbose_name = '节点'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


# 角色表
class RoseModel(models.Model):
    level = models.IntegerField(primary_key=True, unique=True, verbose_name='角色等级')
    name = models.CharField(max_length=50, verbose_name='角色名称')

    class Meta:
        db_table = 'Rose'
        verbose_name = '角色'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


# 用户表
class UserModel(models.Model):
    username = models.CharField(max_length=50, verbose_name='用户名', unique=True)
    password = models.CharField(max_length=50, verbose_name='密码')
    user_level = models.IntegerField(verbose_name='用户等级', default=1)

    class Meta:
        db_table = 'user'
        verbose_name = '用户'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.username


# 用户关联角色表
class UserRoseModel(models.Model):
    user = models.ForeignKey(to=UserModel, on_delete=models.CASCADE, verbose_name='用户外键')
    rose = models.ForeignKey(to=RoseModel, on_delete=models.CASCADE, verbose_name='角色外键')

    class Meta:
        db_table = 'userrose'
        verbose_name = '用户关联角色'
        verbose_name_plural = verbose_name


# 权限表
class JurisdictionModel(models.Model):
    rose = models.ForeignKey(RoseModel, on_delete=models.CASCADE, verbose_name='角色外键')
    node = models.ForeignKey(NodeModel, on_delete=models.CASCADE, verbose_name='节点外键')

    class Meta:
        db_table = 'jurisdiction'
        verbose_name = '权限'
        verbose_name_plural = verbose_name


# 工单分类表
class WorkOrderModel(models.Model):
    id = models.AutoField(primary_key=True, verbose_name='主键id')
    name = models.CharField(max_length=50, verbose_name='工单分类名')
    pid = models.IntegerField(verbose_name='父级id')

    class Meta:
        db_table = 'workorder'
        verbose_name = '工单分类'
        verbose_name_plural = verbose_name


# 分类模板表
# class CateTemplateModel(models.Model):
#     id = models.AutoField(primary_key=True, verbose_name='主键id')
#     workorder = models.ForeignKey(WorkOrderModel, on_delete=models.CASCADE, verbose_name='分类id')
#     fieldlist = models.CharField(max_length=100, verbose_name='字段列表')
#
#     class Meta:
#         db_table = 'catetemplate'
#         verbose_name = '分类模板'
#         verbose_name_plural = verbose_name


# 工单（入库）表
class StorageModel(models.Model):
    StartChoice = (
        (1, '未审核'),
        (2, '审核已通过'),
        (3, '审核未通过'),
    )
    uid = models.ForeignKey(UserModel, on_delete=models.SET_NULL, null=True, blank=True)
    wid = models.ForeignKey(WorkOrderModel, on_delete=models.SET_NULL, null=True, blank=True)
    message = models.CharField(max_length=200, verbose_name='数据信息')
    checklist = models.CharField(max_length=100, null=True, verbose_name='审核人列表', )
    now_check = models.CharField(max_length=50, null=True, verbose_name='当前审核人')
    start = models.IntegerField(verbose_name='审核状态', choices=StartChoice, default=1)
    qnimg = models.CharField(max_length=100, null=True)

    class Meta:
        db_table = 'storage'
        verbose_name = '工单表'
        verbose_name_plural = verbose_name


# 列表嵌套表 -- [[1,2],3]
class NestedList(models.Model):
    sid = models.ForeignKey(StorageModel, on_delete=models.SET_NULL, null=True, blank=True)
    uid = models.IntegerField()

    class Meta:
        db_table = 'nestedlist'
        verbose_name = '列表嵌套表'
        verbose_name_plural = verbose_name


# 消息表
class MessageModel(models.Model):
    name = models.CharField(max_length=50, verbose_name='消息名称')
    wid = models.ForeignKey(WorkOrderModel, on_delete=models.SET_NULL, null=True, blank=True, verbose_name='工单分类id')
    uid = models.ForeignKey(UserModel, on_delete=models.SET_NULL, null=True, blank=True, verbose_name='用户id')
    read = models.BooleanField(default=0)

    class Meta:
        db_table = 'message'
        verbose_name = '消息表'
        verbose_name_plural = verbose_name


# 聊天表
class ChatModel(models.Model):
    send = models.IntegerField(verbose_name='发送人id')
    reception=models.IntegerField(null=True,verbose_name='接收人id')
    name = models.CharField(max_length=100, verbose_name='聊天内容')
    img = models.CharField(max_length=500, null=True, verbose_name='聊天图片')
    read = models.BooleanField(default=0, verbose_name='阅读状态')
    withdraw = models.BooleanField(default=0, verbose_name='撤回状态')

    class Meta:
        db_table = 'chat'
        verbose_name = '聊天表'
        verbose_name_plural = verbose_name

